package com.contest.service.impl;

import com.contest.dao.MerchantInfoDao;
import com.contest.dao.RechargeRecordDao;
import com.contest.dao.UserInfoDao;
import com.contest.entity.MerchantInfo;
import com.contest.entity.RechargeRecord;
import com.contest.entity.UserInfo;
import com.contest.service.BettingRecordService;
import com.contest.service.MerchantInfoService;
import com.contest.service.RechargeRecordService;
import com.contest.service.UserInfoService;
import com.contest.util.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p>
 * ${table.comment}  服务实现类
 * </p>
 *
 * @author liuyf
 * @since ${date}
 */
@Service
public class UserInfoServiceImpl extends  ServiceImpl<UserInfoDao, UserInfo> implements UserInfoService {

    @Autowired
    private MerchantInfoDao merchantInfoDao;

    @Autowired
    private UserInfoDao userInfoDao;

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private RechargeRecordDao rechargeRecordDao;
    @Autowired
    BettingRecordService bettingRecordService;

    /**
     * 查看用户详情
     *
     * @param userId
     */
    @Override
    public UserInfo getUserDetailByUserId(String userId) {
        UserInfo userInfo = this.selectById(userId);
        float today = bettingRecordService.getTodayByUserId(userId);
        float mon = bettingRecordService.getMonByUserId(userId);
        float todayWin = bettingRecordService.getTodayWinByUserId(userId);
        float todayLose = bettingRecordService.getTodayLoseByUserId(userId);
        float monWin = bettingRecordService.getMonWinByUserId(userId);
        float monLose = bettingRecordService.getMonLoseByUserId(userId);
        userInfo.setToday(BigDecimal.valueOf(today));
        userInfo.setMon(BigDecimal.valueOf(mon));
        userInfo.setTodayLose(BigDecimal.valueOf(todayWin - todayLose));
        userInfo.setMonLose(BigDecimal.valueOf(monWin - monLose));
        return userInfo;
    }

    @Override
    @Transactional
    public boolean userRecharge(String amount, String userid, HttpServletRequest request) {
        if(amount == null || amount.trim().length() ==0 || "null".equals(amount)){
            return false;
        }
        if(userid == null || userid.trim().length() ==0 || "null".equals(userid)){
            return false;
        }
        UserInfo userInfo = userInfoDao.selectById(userid);
        userInfo.setFdOver(userInfo.getFdOver().add(new BigDecimal(Integer.parseInt(amount))));
        userInfo.setFdRechargeTotal(userInfo.getFdRechargeTotal().add(new BigDecimal(Integer.parseInt(amount))));
        MerchantInfo merchantInfo = (MerchantInfo) request.getSession().getAttribute("user");
        merchantInfo.setFdOver(merchantInfo.getFdOver().subtract(new BigDecimal(Integer.parseInt(amount))));

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        RechargeRecord rechargeRecord = new RechargeRecord();
        rechargeRecord.setFdId(String.valueOf(idWorker.nextId()));
        rechargeRecord.setFdTime(sdf.format(new Date()));
        rechargeRecord.setFdAmount(new BigDecimal(amount));
        rechargeRecord.setFdUserId(userid);
        rechargeRecord.setFdUserName(userInfo.getFdNickname());
        rechargeRecord.setFdMerchantId(merchantInfo.getFdId());
        rechargeRecord.setFdMerchantName(merchantInfo.getFdName());

        int num = userInfoDao.updateById(userInfo);
        if(num > 0){
            int n = merchantInfoDao.updateById(merchantInfo);
            if(n > 0){
                rechargeRecordDao.insert(rechargeRecord);
                return true;
            }
        }
        return false;
    }
}
